<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Expression;

class UserGroupTable extends AbstractTable
{
    protected $table = 'hycrm_user_group';
    protected $id = 'user_group_id';
    
    public function findAll()
    {
        $rowset = $this->select();
        $result = $rowset->toArray();
        return $result;
    }

    public function findOne($id)
    {
        $rowset = $this->select(array(
            'user_group_id' => $id
        ));
        $row = $rowset->current();
        return $row;
    }

	public function updateItem($id,$data){
		if(empty($id)){
			throw new \Exception("id field is invalid!");
		}
		$item = array(
			'user_group_name'=>$data['user_group_name'],
			'update_time'=>new Expression('NOW()'),
		);
		return $this->update($item,array('user_group_id'=>$id));
	}
	
	public function deleteItem($id){
		if(empty($id)){
			throw new \Exception("id field is invalid!");
		}
		return $this->delete(array('user_group_id'=>$id));
		
	}
	
	public function addItem($data){
		
		$item = array(
			'user_group_name'=>$data['user_group_name'],
			'create_time'=>new Expression('NOW()'),
		    'update_time'=>new Expression('NOW()'),
		);
		

		$this->insert($item);
		$id = $this->lastInsertValue;

		return $id;
	}
	
	public function getGroupList($query = array())
	{
	    $select = new Select(array('g' =>$this->getTable()));
	    $select->order('g.user_group_id');
	     
	    return $this->fetchPaginate($select,$query);
	}
}